/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2025/4/26
 * Time: 9:45
 */

#include "AssetStore.h"
#include <string>

void AssetStore::Clean()
{
    for(auto & texture : textures_) {
        SDL_DestroyTexture(texture.second);
    }
    textures_.clear();

    for(auto & sound : sounds_) {
        Mix_FreeChunk(sound.second);
    }
    sounds_.clear();

    for(auto & music : music_) {
        Mix_FreeMusic(music.second);
    }
    music_.clear();

    for(auto & font : fonts_) {
        TTF_CloseFont(font.second);
    }
    fonts_.clear();
}

void AssetStore::LoadImage(const std::string & _file_path)
{
    SDL_Texture * texture = IMG_LoadTexture(renderer_, _file_path.c_str());
    if(texture == nullptr) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load image: %s;Error: %s",_file_path.c_str(), SDL_GetError());
        return;
    }
    textures_.emplace(_file_path, texture);
}

void AssetStore::LoadSound(const std::string & _file_path)
{
    Mix_Chunk * sound = Mix_LoadWAV(_file_path.c_str());
    if(sound == nullptr) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load sound: %s;Error: %s",_file_path.c_str(), SDL_GetError());
        return;
    }
    sounds_.emplace(_file_path, sound);
}

void AssetStore::LoadMusic(const std::string & _file_path)
{
    Mix_Music * music = Mix_LoadMUS(_file_path.c_str());
    if(music == nullptr) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load music: %s;Error: %s",_file_path.c_str(), SDL_GetError());

        return;
    }
    music_.emplace(_file_path, music);
}

void AssetStore::LoadFont(const std::string & _file_path, int _font_size)
{
    TTF_Font * font = TTF_OpenFont(_file_path.c_str(), _font_size);
    if(font == nullptr) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load font: %s;Error: %s",_file_path.c_str(), SDL_GetError());
        return;
    }
    fonts_.emplace(_file_path + std::to_string(_font_size), font);
}

SDL_Texture * AssetStore::GetImage(const std::string & _file_path)
{
    auto iter = textures_.find(_file_path);
    if(iter == textures_.end()) {
        LoadImage(_file_path);
        iter = textures_.find(_file_path);
    }
    if(iter == textures_.end()) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get image: %s", _file_path.c_str());
        return nullptr;
    }
    return iter->second;
}

Mix_Chunk * AssetStore::GetSound(const std::string & _file_path)
{
    auto iter = sounds_.find(_file_path);
    if(iter == sounds_.end()) {
        LoadSound(_file_path);
        iter = sounds_.find(_file_path);
    }
    if(iter == sounds_.end()) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get sound: %s", _file_path.c_str());
        return nullptr;
    }
    return iter->second;
}

Mix_Music * AssetStore::GetMusic(const std::string & _file_path)
{
    auto iter = music_.find(_file_path);
    if(iter == music_.end()) {
        LoadMusic(_file_path);
        iter = music_.find(_file_path);
    }
    if(iter == music_.end()) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get music: %s", _file_path.c_str());
        return nullptr;
    }
    return iter->second;
}

TTF_Font * AssetStore::GetFont(const std::string & _file_path, int _font_size)
{
    auto iter = fonts_.find(_file_path + std::to_string(_font_size));
    if(iter == fonts_.end()) {
        LoadFont(_file_path, _font_size);
        iter = fonts_.find(_file_path + std::to_string(_font_size));
    }
    if(iter == fonts_.end()) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get font: %s", _file_path.c_str());
        return nullptr;
    }
    return iter->second;
}